package com.h3c.nem.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eos.common.util.IdWorker;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.h3c.nem.entity.TbWorkExperience;
import com.h3c.nem.mapper.TbWorkExperienceMapper;
import com.h3c.nem.service.ITbWorkExperienceService;
import com.h3c.nem.dto.TbWorkExperienceDTO;
import com.h3c.nem.utils.InitExampleOrderByUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * Copyright (C), 2020-2020, h3c
 * FileName: TbWorkExperienceServiceImpl
 *
 * @Author: l22886
 * @Date: 2022-01-19
 * Description: tb_work_experience serviceImpl
 * Version: 1.0
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class TbWorkExperienceServiceImpl extends ServiceImpl<TbWorkExperienceMapper, TbWorkExperience> implements ITbWorkExperienceService {

    @Resource
    private TbWorkExperienceMapper tbWorkExperienceMapper;



    /**
     * @title: selectFuzzyByDto
     * @description: 模糊查询操作
     * @params: [ dto ]
     * @return: java.util.List
     * @createTime: 2022-01-19
     * @version: 1.0
     * @author: l22886
     */
    @Override
    public List<TbWorkExperienceDTO> selectFuzzyByDto(TbWorkExperienceDTO dto) {
        //List<TbWorkExperience> list = tbWorkExperienceMapper.selectByExample(initFuzzyExample(dto));
        List<TbWorkExperience> list = tbWorkExperienceMapper.queryListByFuzzy(JSONObject.parseObject(JSONObject.toJSONString(dto),TbWorkExperience.class));
        return JSONArray.parseArray(JSONArray.toJSONString(list), TbWorkExperienceDTO.class);
    }

    /**
     * @title: selectByDto
     * @description: 精确查询操作
     * @params: [ dto ]
     * @return: java.util.List
     * @createTime: 2022-01-19
     * @version: 1.0
     * @author: l22886
     */
    @Override
    public List<TbWorkExperienceDTO> selectByDto(TbWorkExperienceDTO dto) {
        //List<TbWorkExperience> list = tbWorkExperienceMapper.selectByExample(initExample(dto));
        List<TbWorkExperience> list = tbWorkExperienceMapper.queryList(JSONObject.parseObject(JSONObject.toJSONString(dto),TbWorkExperience.class));
        return JSONArray.parseArray(JSONArray.toJSONString(list), TbWorkExperienceDTO.class);
    }

    /**
     * @title: insertDto
     * @description: 插入操作
     * @params: [ dto ]
     * @return: Integer
     * @createTime: 2022-01-19
     * @version: 1.0
     * @author: l22886
     */
    @Override
    public Integer insertDto(TbWorkExperienceDTO dto) {
        //if (null == dto.getId() || "".equals(dto.getId())) {
        //    String pkId = IdWorker.nextId() + "";
        //    dto.setId(pkId);
        //}
        return tbWorkExperienceMapper.insertTbWorkExperience(JSONObject.parseObject(JSON.toJSONString(dto), TbWorkExperience.class));
    }

    /**
     * @title: updateDto
     * @description: 修改操作
     * @params: [ dto ]
     * @return: Integer
     * @createTime: 2022-01-19
     * @version: 1.0
     * @author: l22886
     */
    @Override
    public Integer updateDto(TbWorkExperienceDTO dto) {
        //if (null != dto && null != dto.getId() && !"".equals(dto.getId())) {
            return tbWorkExperienceMapper.updateTbWorkExperience(JSONObject.parseObject(JSON.toJSONString(dto), TbWorkExperience.class));
            //return tbWorkExperienceMapper.updateByPrimaryKey(JSONObject.parseObject(JSON.toJSONString(dto), TbWorkExperience.class));
        //} else {
        //    return 0;
        //}
    }

    /**
     * @title: deleteDto
     * @description: 删除操作
     * @params: [ dto ]
     * @return: Integer
     * @createTime: 2022-01-19
     * @version: 1.0
     * @author: l22886
     */
    @Override
    public Integer deleteDto(TbWorkExperienceDTO dto) {
        //if (null != dto && null != dto.getId() && !"".equals(dto.getId())) {
            return tbWorkExperienceMapper.deleteTbWorkExperience(JSONObject.parseObject(JSON.toJSONString(dto), TbWorkExperience.class));
            //return tbWorkExperienceMapper.deleteByPrimaryKey(dto.getId());
        //} else {
        //    return 0;
        //}
    }


    /**
     * @title: initFuzzyExample
     * @description: 初始化 模糊查询 example
     * @params: [ dto ]
     * @return: tk.mybatis.mapper.entity.Example
     * @createTime: 2022-01-19
     * @version: 1.0
     * @author: l22886
     */
    private Example initFuzzyExample(TbWorkExperienceDTO dto) {
        Example example = InitExampleOrderByUtils.initExampleOrderBy(TbWorkExperience.class, dto.getOrderBy());
        Example.Criteria criteriaName = example.createCriteria();
        if (null != dto) {
            if (null != dto.getRecordId() && !"".equals(dto.getRecordId())) {
                criteriaName.andLike("RecordId", "%" + dto.getRecordId() + "%");
            }
            if (null != dto.getCompanyName() && !"".equals(dto.getCompanyName())) {
                criteriaName.andLike("CompanyName", "%" + dto.getCompanyName() + "%");
            }
            if (null != dto.getDeptName() && !"".equals(dto.getDeptName())) {
                criteriaName.andLike("DeptName", "%" + dto.getDeptName() + "%");
            }
            if (null != dto.getBeginDate() && !"".equals(dto.getBeginDate())) {
                criteriaName.andLike("BeginDate", "%" + dto.getBeginDate() + "%");
            }
            if (null != dto.getEndDate() && !"".equals(dto.getEndDate())) {
                criteriaName.andLike("EndDate", "%" + dto.getEndDate() + "%");
            }
            if (null != dto.getPostName() && !"".equals(dto.getPostName())) {
                criteriaName.andLike("PostName", "%" + dto.getPostName() + "%");
            }
            if (null != dto.getWorkCity() && !"".equals(dto.getWorkCity())) {
                criteriaName.andLike("WorkCity", "%" + dto.getWorkCity() + "%");
            }
            if (null != dto.getLeaveReason() && !"".equals(dto.getLeaveReason())) {
                criteriaName.andLike("LeaveReason", "%" + dto.getLeaveReason() + "%");
            }
        }
        return example;
    }

    /**
     * @title: initExample
     * @description: 初始化 精确查询 example
     * @params: [ dto ]
     * @return: tk.mybatis.mapper.entity.Example
     * @createTime: 2022-01-19
     * @version: 1.0
     * @author: l22886
     */
    private Example initExample(TbWorkExperienceDTO dto) {
        Example example = InitExampleOrderByUtils.initExampleOrderBy(TbWorkExperience.class, dto.getOrderBy());
        Example.Criteria criteriaName = example.createCriteria();
        if (null != dto) {
            if (null != dto.getRecordId() && !"".equals(dto.getRecordId())) {
                criteriaName.andEqualTo("RecordId", dto.getRecordId());
            }
            if (null != dto.getCompanyName() && !"".equals(dto.getCompanyName())) {
                criteriaName.andEqualTo("CompanyName", dto.getCompanyName());
            }
            if (null != dto.getDeptName() && !"".equals(dto.getDeptName())) {
                criteriaName.andEqualTo("DeptName", dto.getDeptName());
            }
            if (null != dto.getBeginDate() && !"".equals(dto.getBeginDate())) {
                criteriaName.andEqualTo("BeginDate", dto.getBeginDate());
            }
            if (null != dto.getEndDate() && !"".equals(dto.getEndDate())) {
                criteriaName.andEqualTo("EndDate", dto.getEndDate());
            }
            if (null != dto.getPostName() && !"".equals(dto.getPostName())) {
                criteriaName.andEqualTo("PostName", dto.getPostName());
            }
            if (null != dto.getWorkCity() && !"".equals(dto.getWorkCity())) {
                criteriaName.andEqualTo("WorkCity", dto.getWorkCity());
            }
            if (null != dto.getLeaveReason() && !"".equals(dto.getLeaveReason())) {
                criteriaName.andEqualTo("LeaveReason", dto.getLeaveReason());
            }
        }
        return example;
    }


}
